package com.lzb.compressmaster.adapter;

import android.text.format.Formatter;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.ProviderMultiAdapter;
import com.lzb.compressmaster.basic.media.MediaInfo;
import com.lzb.compressmaster.utils.MimeUtils;

import java.io.File;
import java.util.List;


public class DocumentsAdapter extends ProviderMultiAdapter<MediaInfo> {

    private boolean isEdit;

    public DocumentsAdapter() {
        addItemProvider(new DocumentsItemProvider());
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private class DocumentsItemProvider extends BaseItemProvider<MediaInfo> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_documents;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MediaInfo mediaInfo) {
            ImageView ivDocumentsIcon = baseViewHolder.getView(R.id.ivDocumentsIcon);
            if (FileUtils.isDir(mediaInfo.getPath())) {
                ivDocumentsIcon.setImageResource(R.drawable.awjj);
            } else if (MimeUtils.isImgMimeType(mediaInfo.getPath())) {
                Glide.with(ivDocumentsIcon).load(mediaInfo.getPath()).into(ivDocumentsIcon);
            } else if (MimeUtils.isVideoMimeType(mediaInfo.getPath())) {
                ivDocumentsIcon.setImageResource(R.drawable.aship);
            } else if (MimeUtils.isAudioMimeType(mediaInfo.getPath())) {
                ivDocumentsIcon.setImageResource(R.drawable.ayinp);
            } else if (mediaInfo.getPath().endsWith(".doc") || mediaInfo.getPath().endsWith(".docx")) {
                ivDocumentsIcon.setImageResource(R.drawable.awend);
            } else if (mediaInfo.getPath().endsWith(".xls") || mediaInfo.getPath().endsWith(".xlsx")) {
                ivDocumentsIcon.setImageResource(R.drawable.awend);
            } else if (mediaInfo.getPath().endsWith(".ppt") || mediaInfo.getPath().endsWith(".pptx")) {
                ivDocumentsIcon.setImageResource(R.drawable.awend);
            } else if (mediaInfo.getPath().endsWith(".zip") || mediaInfo.getPath().endsWith(".7z") || mediaInfo.getPath().endsWith(".tar") ||
                    mediaInfo.getPath().endsWith(".gz") || mediaInfo.getPath().endsWith(".bz2") || mediaInfo.getPath().endsWith(".xz") ||
                    mediaInfo.getPath().endsWith(".ar") || mediaInfo.getPath().endsWith(".cpio") || mediaInfo.getPath().endsWith(".jar")) {
                ivDocumentsIcon.setImageResource(R.drawable.ayasb);
            } else if (mediaInfo.getPath().endsWith(".pdf")) {
                ivDocumentsIcon.setImageResource(R.drawable.awend);
            } else if (mediaInfo.getPath().endsWith(".txt")) {
                ivDocumentsIcon.setImageResource(R.drawable.awend);
            } else {
                ivDocumentsIcon.setImageResource(R.drawable.aweizwnj);
            }
            if (isEdit) {
                if (mediaInfo.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivDocumentsDetails, R.drawable.axuanz);
                } else {
                    baseViewHolder.setImageResource(R.id.ivDocumentsDetails, R.drawable.awxz);
                }
            } else {
                baseViewHolder.setImageResource(R.id.ivDocumentsDetails, R.drawable.ajr);
            }

            baseViewHolder.setText(R.id.tvDocumentsName, mediaInfo.getName());
            String phoneTime = TimeUtils.millis2String(mediaInfo.getDateModified(), "yyyy-MM-dd HH:mm:ss");
            baseViewHolder.setText(R.id.tvDocumentsDate, phoneTime);
            if (FileUtils.isDir(mediaInfo.getPath())) {
                List<File> files = FileUtils.listFilesInDir(mediaInfo.getPath());
                baseViewHolder.setText(R.id.tvDocumentsSize, files.size() + getContext().getString(R.string.folder_size_unit));
            } else {
                baseViewHolder.setText(R.id.tvDocumentsSize, Formatter.formatFileSize(context, mediaInfo.getSize()));
            }
        }
    }
}
